package com.jkc.xh.fragments;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.jkc.xh.R;
import com.jkc.xh.adapters.MyFactoryAdapter;
import com.jkc.xh.adapters.core.BaseRecyclerAdapter;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.fragments.core.BasePullRecyclerFragment;
import com.jkc.xh.manager.TraderManager;
import com.jkc.xh.manager.UserManager;
import com.jkc.xh.models.HoldOneModel;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.HoldOneResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.network.models.SystemHintResponse;
import com.jkc.xh.views.widget.ToastUtil;
import com.jkc.xh.views.widget.pullview.PullRecyclerView;

import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 我的持仓-分笔持仓
 */
@EFragment(R.layout.fragment_child_my_factory)
public class MyFactoryChildFragment extends BasePullRecyclerFragment implements BaseRecyclerAdapter.OnViewClickListener {

    private MyFactoryAdapter mAdapter;
    private int pageIndex = 0;
    private int pageTotal = 0;
    @ViewById
    TextView hintInfo;
    @Override
    protected void initView(PullRecyclerView recyclerView) {
        mAdapter = new MyFactoryAdapter();
        mAdapter.setListener(this);
        mAdapter.setItemClickListener(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);
        requestData(true);

        addJob(UserManager.getCpmfHintConfig(new RestCallBack<SystemHintResponse>() {
            @Override
            public void success(SystemHintResponse obj) {
                hintInfo.setText(Html.fromHtml(obj.about_yqfy));
            }

            @Override
            public void failure(RestError error) {

            }
        }));
    }

    public void requestData(final boolean isRefresh) {
        if (isRefresh) {
            pageIndex = 0;
            if (getPullRecyclerView() != null) {
                getPullRecyclerView().autoRefresh();
            }
        } else {
            pageIndex++;
            if (pageIndex >= pageTotal) {
                return;
            }
        }
        addJob(TraderManager.queryHoldOneList(pageIndex,0, new RestCallBack<HoldOneResponse>() {
            @Override
            public void success(HoldOneResponse response) {
                pageTotal = response.pages;
                if (response.list == null || response.list.size() == 0) {
//                    ToastUtil.showToastShort("数据加载完毕");
                    finishLoad(false);
                    return;
                }
                if (mAdapter == null) {
                    return;
                }
                if (isRefresh) {
                    mAdapter.replaceWith(response.list);
                } else {
                    mAdapter.addAll(response.list);
                }
                if (response.list.size() < Constant.PAGE_SIZE) {
                    finishLoad(false);
                } else {
                    finishLoad(true);
                }
            }

            @Override
            public void failure(RestError error) {

            }
        }));

    }

    @Override
    protected void onRefresh(RecyclerView recyclerView) {
        requestData(true);
    }

    @Override
    protected void onLoadMore(RecyclerView recyclerView) {
        requestData(false);
    }

    @Override
    public void onItemClick(View view, Object object, int position) {
        HoldOneModel model = (HoldOneModel) object;
        switch (view.getId()) {
            case R.id.btnSold:
                if (model.isStockCanSell()) {
                    TradeSoldPartFragment_.builder().arg("modelExtra", model).build().startFragment(getContext());
                } else {
                    ToastUtil.showToastShort("该持仓不可卖!");
                }
                break;
            case R.id.auto_deposit:
                CheckBox cb_deposit = (CheckBox) view;
                int deposit = cb_deposit.isChecked() == true ? 1 : 0;
                showProgress();
                addJob(TraderManager.autoDeposit(model.id, deposit, new RestCallBack<Object>() {
                    @Override
                    public void success(Object o) {
                        hideProgress();
                        ToastUtil.showToastShort("操作成功");
                    }

                    @Override
                    public void failure(RestError error) {
                        hideProgress();
                        ToastUtil.showToastShort(error.msg);
                        cb_deposit.setChecked(!cb_deposit.isChecked());
                    }
                }));
                break;
            case R.id.auto_delay:
                CheckBox cb_delay = (CheckBox) view;
                int delay = cb_delay.isChecked() == true ? 1 : 0;
                showProgress();
                addJob(TraderManager.autoDelay(model.id, delay, new RestCallBack<Object>() {
                    @Override
                    public void success(Object o) {
                        hideProgress();
                        ToastUtil.showToastShort("操作成功");
                    }

                    @Override
                    public void failure(RestError error) {
                        hideProgress();
                        ToastUtil.showToastShort(error.msg);
                        cb_delay.setChecked(!cb_delay.isChecked());
                    }
                }));
                break;
            case R.id.toHaltDpt:
                if (!model.isDoHaltdpt()) {
                    showProgress();
                    addJob(TraderManager.haltDpt(model.id, 1, new RestCallBack<Object>() {
                        @Override
                        public void success(Object o) {
                            hideProgress();
                            ToastUtil.showToastShort("操作成功");
                            model.haltdpt_flag = 1;
                            TextView tv = (TextView) view;
                            tv.setText("已补保证金降息");
                            tv.setTextColor(getResources().getColor(R.color.text_color_gray));
                            tv.setBackgroundResource(R.drawable.rect_dashed_white);
                        }

                        @Override
                        public void failure(RestError error) {
                            hideProgress();
                            ToastUtil.showToastShort(error.msg);
                        }
                    }));
                }
                break;
        }
    }
}
